package designMode.compoundPattern;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author LIBOFEI
 * @ClassName Observable
 * @description  Observable实现了所有必要的功能，我们只要把它插进一个类，就可以让该类工作委托给Observable
 * @date 2020/7/1 15:49
 * @Version 1.0
 **/
public class Observable implements QuackObservable {

    List<Observer> observers = new ArrayList<>();
    QuackObservable duck;

    public Observable(QuackObservable duck){
        this.duck = duck;
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void notifyObservers() {
        Iterator<Observer> iterator = observers.iterator();
        while(iterator.hasNext()){
            Observer observer = iterator.next();
            observer.update(duck);
        }
    }
}
